package com.egene.tw.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.model.HealthItem;
import com.egene.tw.utils.ImageUtil;

import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * Created by WZX on 2015/9/8.
 */
@EViewGroup(R.layout.view_health)
public class HealthView extends LinearLayout {
    @ViewById
    ImageView ivHead;
    @ViewById
    TextView tvTitle;
    @ViewById
    LinearLayout ll_container;

    LayoutInflater inflater;
    OnItemClickListener mListener;

    boolean showImg;

    public HealthView(Context context) {
        this(context, null);
    }

    public HealthView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflater = LayoutInflater.from(getContext());
    }
//
//    public void showImage(boolean show){
//        showImg = show;
//    }

    public void setData(int head, String title, List<HealthItem> list){
        ImageUtil.loadImage(getContext(), ivHead, head);
        tvTitle.setText(title);
        if(list != null && list.size() > 0){
            final HealthItem item = list.get(0);
            View view = inflater.inflate(R.layout.item_health, ll_container, false);
            ImageView ivHead = (ImageView) view.findViewById(R.id.ivHead);
            TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
            TextView tvContent = (TextView) view.findViewById(R.id.tvContent);

//
//            ivHead.setVisibility(showImg? VISIBLE:GONE);

            ImageUtil.loadImage(getContext(), ivHead, item.getSmallimg());
            tvTitle.setText(item.getTitle());
            tvContent.setText(item.getZhaoyao());
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mListener != null){
                        mListener.onItemClick(item);
                    }
                }
            });

            ll_container.addView(view);
        }
//
//
//        for(final HealthItem item : list){
//            View view = inflater.inflate(R.layout.item_health, ll_container, false);
//            ImageView ivHead = (ImageView) view.findViewById(R.id.ivHead);
//            TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
//            TextView tvContent = (TextView) view.findViewById(R.id.tvContent);
//
//            ImageUtil.loadImage(getContext(), ivHead, item.getSmallimg());
//            tvTitle.setText(item.getTitle());
//            tvContent.setText(item.getZhaoyao());
//            view.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if(mListener != null){
//                        mListener.onItemClick(item);
//                    }
//                }
//            });
//
//            ll_container.addView(view);
//        }

    }

    public void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
    }

    public interface OnItemClickListener{
        void onItemClick(HealthItem item);
    }

}
